//
//  ControllerSlashGameScene.cpp
//  CanYouDoIt
//
//  Created by hoangtx on 7/15/13.
//
//

#include "ControllerSlashGameScene.h"
#include "GameLayerManager.h"
#include "SceneManager.h"
#include "MyUtilities.h"
CCScene* ControllerSlashGameScene::scene(int numberOfSeconds){
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    ControllerSlashGameScene *layer = ControllerSlashGameScene::create(numberOfSeconds);
    GameLayerManager::sharedInstance()->controllerSplashGameLayer = layer;
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

bool ControllerSlashGameScene::init(){
    CountDownGameScene::init();
    fileNameArray = CCArray::create();
    fileNameArray->retain();
    fileNameArray->addObject(CCString::create("button_cross.png"));
    fileNameArray->addObject(CCString::create("button_circle.png"));
    fileNameArray->addObject(CCString::create("button_square.png"));
    fileNameArray->addObject(CCString::create("button_triangle.png"));
    
    
    
    buttonArray = CCArray::create();
    buttonArray->retain();
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    double buttonSize = winSize.width*0.2;
    double offsetX = (winSize.width - 2*buttonSize)/3;
    double offsetY = offsetX*0.25f;
    for(int i=0;i<4;i++){
        CCString* iFileName = (CCString*)fileNameArray->objectAtIndex(i);
        int row = i/2;
        int column = i%2;
        MyControllerButton* iButton = MyControllerButton::create("circle_button.png", iFileName->getCString(), (column+1)*offsetX+column*buttonSize,
                                                         (row+1)*offsetY+row*buttonSize, buttonSize, buttonSize);
        CCCallFuncO* iCallBack = CCCallFuncO::create(this,callfuncO_selector(ControllerSlashGameScene::onTapAtButton), CCInteger::create(i));
        iButton->setColorState(ccc3(50, 50, 50), ccc3(150, 150, 150));
        iButton->addCallback(iCallBack);
        setColorStateForButton(iButton, i);
        this->addChild(iButton);
        buttonArray->addObject(iButton);
    }
    correctArray = CCArray::create();
    correctArray->retain();
    targetArray = CCArray::create();
    targetArray->retain();
    subTargetArray = CCArray::create();
    subTargetArray->retain();
    isLock = false;
    
    return true;
}

ControllerSlashGameScene* ControllerSlashGameScene::create(int numberOfSeconds){
    ControllerSlashGameScene* scene = ControllerSlashGameScene::create();
    scene->maxTimeCountDown = numberOfSeconds;
    scene->timeRemaining = scene->maxTimeCountDown;
    scene->scoreAndTimeLayer->updateTime(scene->timeRemaining);
    return scene;
}

void ControllerSlashGameScene::setColorStateForButton(MyControllerButton *iButton, int index){
    switch (index) {
        case 0:{
            iButton->spriteOnTop->setColor(ccc3(164, 194, 237));
        }
            break;
            
        case 1:{
            iButton->spriteOnTop->setColor(ccc3(237, 140, 142));
        }
            break;
        case 2:{
            iButton->spriteOnTop->setColor(ccc3(240, 180, 224));
            
        }
            break;
        case 3:{
            iButton->spriteOnTop->setColor(ccc3(0, 222, 218));
            
        }
            break;
    }
}

void ControllerSlashGameScene::lock(){
    isLock = true;
    this->schedule(schedule_selector(ControllerSlashGameScene::tickTimeLock), 1, 0, 0);
}

void ControllerSlashGameScene::tickTimeLock(float dt){
    this->unschedule(schedule_selector(OrderTapGameScene::tickTimeLock));
    isLock=false;
    int highLightIndex = numberOfTap % 4;
    MyControllerButton* highLightButton = (MyControllerButton*)targetArray->objectAtIndex(highLightIndex);
    highLightButton->goToNormalMode();
}

void ControllerSlashGameScene::randomizeTarget(cocos2d::CCArray *array, double startY){
    if(array==NULL){
        array = CCArray::create();
        array->retain();
    }
    for(int i=0;i<array->count();i++){
        CCNode* iNode = (CCNode*)array->objectAtIndex(i);
        iNode->removeFromParent();
    }
    array->removeAllObjects();
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    double buttonSize = winSize.width*0.2;
    double offset = buttonSize*0.1f;
    double rowWidth = buttonSize*4+offset*5;
    double startX = (winSize.width-rowWidth)/2;
    int removeButton = rand()%4;
    for(int i=0;i<4;i++){
        
        int randomButton;// = rand()%4;
        do {
            randomButton = rand()%4;
        } while (randomButton==removeButton);
        CCString* iFileName = (CCString*)fileNameArray->objectAtIndex(randomButton);
        MyControllerButton* iButton = MyControllerButton::create("circle_button.png", iFileName->getCString(), startX+(i+1)*offset + i*buttonSize,
                                                                 startY, buttonSize, buttonSize);
        iButton->setColorState(ccc3(50, 50, 50), ccc3(150, 150, 150));
        setColorStateForButton(iButton, randomButton);
        this->addChild(iButton);
        array->addObject(iButton);
        correctArray->addObject(CCInteger::create(randomButton));
    }
}

void ControllerSlashGameScene::swapAndContinueToRandomize(){
    for(int i=0;i<subTargetArray->count();i++){
        MyControllerButton* iButton = (MyControllerButton*)subTargetArray->objectAtIndex(i);
        MyControllerButton* targetButton = (MyControllerButton*)targetArray->objectAtIndex(i);
        iButton->removeFromParent();
        targetButton->spriteOnTop->setTexture(iButton->spriteOnTop->getTexture());
        targetButton->spriteOnTop->setColor(iButton->spriteOnTop->getColor());
        targetButton->goToNormalMode();
    }
    randomizeTarget(subTargetArray, getTargetTop());
}

double ControllerSlashGameScene::getTargetBot(){
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    double buttonSize = winSize.width*0.2;
    double offset = buttonSize*0.2f;
    return getTargetTop()-buttonSize-offset;
}
double ControllerSlashGameScene::getTargetTop(){
    CCSize winSize=  CCDirector::sharedDirector()->getWinSize();
    return (winSize.height-scoreAndTimeLayer->getBarHeight())*0.8f;
}

CCRect ControllerSlashGameScene::getMainTargetRect(){
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    double buttonSize = winSize.width*0.2;
    double offset = buttonSize*0.1f;
    double rowWidth = buttonSize*4+offset*5;
    double startX = (winSize.width-rowWidth)/2;
    double startY = getTargetBot();
    return CCRectMake(startX, startY, rowWidth, buttonSize);
}

void ControllerSlashGameScene::startGame(){
    CountDownGameScene::startGame();
    SceneManager::sharedInstance()->previousScene = SceneManager::sharedInstance()->controllerSplashGameScene;
    randomizeTarget(targetArray, getTargetBot());
    randomizeTarget(subTargetArray, getTargetTop());
}

void ControllerSlashGameScene::onTapAtButton(cocos2d::CCObject *sender){
    CCInteger* correctTapInteger = (CCInteger*)correctArray->objectAtIndex(0);
    int correctTap = correctTapInteger->getValue();
    CCInteger* userTapInteger = (CCInteger*)sender;
    int userTap = userTapInteger->getValue();
    
    int highLightIndex = numberOfTap % 4;
    MyControllerButton* highLightButton = (MyControllerButton*)targetArray->objectAtIndex(highLightIndex);
    if(userTap == correctTap){
        correctArray->removeObject(correctTapInteger);

        highLightButton->goToHighlightMode();
        numberOfTap++;
        scoreAndTimeLayer->updateScore(numberOfTap);
        if(numberOfTap%4==0){
            swapAndContinueToRandomize();
        }
    }else{
        lock();
        highLightButton->goToDisabledMode();
    }
}

void ControllerSlashGameScene::reset(){
    CountDownGameScene::reset();
    for(int i=0;i<subTargetArray->count();i++){
        MyControllerButton* iButton = (MyControllerButton*)subTargetArray->objectAtIndex(i);
        MyControllerButton* targetButton = (MyControllerButton*)targetArray->objectAtIndex(i);
        iButton->removeFromParent();
        targetButton->removeFromParent();

    }
    subTargetArray->removeAllObjects();
    targetArray->removeAllObjects();
    correctArray->removeAllObjects();
    isLock=false;
}

void ControllerSlashGameScene::draw(){
    CountDownGameScene::draw();
    MyUtilities::drawRect(getMainTargetRect());
}

void ControllerSlashGameScene::ccTouchesBegan(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    if (isLock) {
        return;
    }
    for(int i=0;i<buttonArray->count();i++){
        MyControllerButton* iButton = (MyControllerButton*)buttonArray->objectAtIndex(i);
        if(iButton->MyButton::isTouchedAt(touches)){

                iButton->runAction(CCSequence::create(iButton->callback,NULL));
                iButton->isFocused=false;
            
            
        }
    }
}

void ControllerSlashGameScene::ccTouchesMoved(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
//    for(int i=0;i<buttonArray->count();i++){
//        MyButton* iButton = (MyButton*)buttonArray->objectAtIndex(i);
//        iButton->handleMultiTouchesMoved(touches, pEvent);
//    }
}

void ControllerSlashGameScene::ccTouchesEnded(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
//    for(int i=0;i<buttonArray->count();i++){
//        MyButton* iButton = (MyButton*)buttonArray->objectAtIndex(i);
//        iButton->handleMultiTouchesEnded(touches, pEvent);
//    }
}
